package example.btl2capecho;


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class ClientCanvas extends GameCanvas implements Runnable {

    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int currentX, currentY; // To hold current position of the 'X'
    private int currentX2, currentY2; // To hold current position of the 'X'
    private int width; // To hold screen width
    private int height; // To hold screen height
    private int speed;
// Constructor and initialization
    MIDletApplication midlet;
    ClientForm clientForm;
    
    public ClientCanvas(MIDletApplication midlet, ClientForm clientForm) {
        super(true);
        this.midlet = midlet;
        this.clientForm = clientForm;
        
        width = getWidth();
        height = getHeight();
        currentX = width / 2;
        currentY = height / 2;
        currentX2 = width / 4 / 2;
        currentY2 = height / 2;
        delay = 20;
        speed = 5;
    }
// Automatically start thread for game loop

    public void start() {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void stop() {
        isPlay = false;
    }
// Main Game Loop

    public void run() {
        Graphics g = getGraphics();
        while (isPlay == true) {
            input();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
            }
        }
    }
// Method to Handle User Inputs

    private void input() {
        int keyStates = getKeyStates();
// Left
        if ((keyStates & LEFT_PRESSED) != 0) {
            currentX = Math.max(0, currentX - speed);
        }
// Right
        if ((keyStates & RIGHT_PRESSED) != 0) {
            if (currentX + 5 < width) {
                currentX = Math.min(width, currentX + speed);
            }
        }
// Up
        if ((keyStates & UP_PRESSED) != 0) {
            currentY = Math.max(0, currentY - speed);
        }
// Down
        if ((keyStates & DOWN_PRESSED) != 0) {
            if (currentY + 10 < height) {
                currentY = Math.min(height, currentY + speed);
            }
        }
    }
// Method to Display Graphics

    private void drawScreen(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);
        clientForm.sendMessage(clientForm.getAddress());
        g.drawString("X" + clientForm.getAddress(), currentX, currentY, Graphics.TOP | Graphics.LEFT);

        g.setColor(0xff00ff);
        currentX2 = currentX / 4;
        g.drawString("^", currentX2, currentY2, Graphics.TOP | Graphics.LEFT);

        g.setColor(0xff0000);
        g.drawRect(0, 0, width / 4, getHeight());
        flushGraphics();
    }

    public void paint(Graphics g) {
        super.paint(g);
        setFullScreenMode(true);
    }

    protected void pointerPressed(int x, int y) {
        this.currentX = x;
        this.currentY = y;
        //input();
    }

    protected void pointerDragged(int x, int y) {
        this.currentX = x;
        this.currentY = y;
        //input();
    }

    protected void pointerReleased(int x, int y) {
        //this.currentX = x;
        //this.currentY = y;        
        //input();
        //scroll
    }
}